package com.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.domain.Product;
import com.interfaces.IProductDAO;

@Repository("productDAO")
public class ProductDAO extends HibernateDAO<Product, Integer> implements IProductDAO{

	@Override
	public List<String> getProductSoorten() {
		Query query = currentSession().createQuery(
                "select p.soort from product p");
		@SuppressWarnings("rawtypes")
		List list = query.list();
		List<String> retval =  new ArrayList<String>();
		for(Object e : list){
			if(e instanceof String){
				retval.add(e.toString());
			}
		}
		return retval;
	}

	@Override
	public Product getProductByCode(int productCode) {
		Query query = currentSession().createQuery(
                "from product p where p.code=:code");
        query.setInteger("code", productCode);
		return (Product)query.uniqueResult();
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Product> getProductLijst(String soort) {
		Query query = (soort != null && soort.equals(""))?currentSession().createQuery(
                "from product p where p.soort=:soort").setString("soort", soort):currentSession().createQuery(
                        "from product p");
                
		return (List<Product>)query.list();
	}

}
